﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

using LP.Entities;
using MySoft.Data;
using MySoft.Web;

namespace MyLearningPlanner
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                rpt.DataSource = DbSession.Default.From<LearningPlan>().ToList();
                rpt.DataBind();

                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                Uri url = Request.Url;

                sb.AppendFormat("AbsolutePath:{0}<br/>", url.AbsolutePath)
                    .AppendFormat("AbsoluteUri:{0}<br/>", url.AbsoluteUri)
                    .AppendFormat("Authority:{0}<br/>", url.Authority)
                    .AppendFormat("DnsSafeHost:{0}<br/>", url.DnsSafeHost)
                    .AppendFormat("Fragment:{0}<br/>", url.Fragment)
                    .AppendFormat("Host:{0}<br/>", url.Host)
                    .AppendFormat("HostNameType:{0}<br/>", url.HostNameType)
                    .AppendFormat("LocalPath:{0}<br/>", url.LocalPath)
                    .AppendFormat("OriginalString:{0}<br/>", url.OriginalString)
                    .AppendFormat("PathAndQuery:{0}<br/>", url.PathAndQuery)
                    .AppendFormat("Query:{0}<br/>", url.Query)
                    .AppendFormat("Scheme:{0}<br/>", url.Scheme)
                    .AppendFormat("Segments:{0}<br/>", url.Segments)
                    .AppendFormat("PathWithoutQuery:{0}", GetUrlWithoutQuery(url));

                ltlInfo.Text = sb.ToString();
            }
        }

        protected void Post(object sender, EventArgs e)
        {
            LearningPlan plan = new LearningPlan();

            plan.Title = txtPlanTitle.Text.Trim();
            plan.StartTime = DateTime.Now;
            plan.EstimateEndTime = DateTime.Now.AddDays(int.Parse(txtEstimateTime.Text));
            plan.Description = txtContent.Text.Trim();

            DbSession.Default.Save(plan);

            Response.Redirect("default.aspx");
        }

        protected string CutStr(object str, int length)
        {
            return str.ToString();
        }

        string GetUrlWithoutQuery(Uri url)
        {
            return string.Format("{0}://{1}{2}", url.Scheme, url.Authority, url.AbsolutePath);
        }
    }
}
